package edu.zjut.day3.aopframework;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;

import edu.zjut.day3.Advise;

public class ProxyFactoryBean {

	private Advise advise;
	private Object target;
	
	
	public Advise getAdvise() {
		return advise;
	}


	public void setAdvise(Advise advise) {
		this.advise = advise;
	}


	public Object getTarget() {
		return target;
	}


	public void setTarget(Object target) {
		this.target = target;
	}


	public Object getProxy() {
		Object proxy3 = Proxy.newProxyInstance(Collection.class.getClassLoader(), 
				/*new Class[]{Collection.class},*/
				target.getClass().getInterfaces(),
				new InvocationHandler(){
					
					@Override
					public Object invoke(Object proxy, Method method, Object[] args)
								throws Throwable {
					/*	long beginTime = System.currentTimeMillis();
						Object retVal  = method.invoke(target,args);
						long endTime = System.currentTimeMillis();
						System.out.println(method.getName()+" running time of "+(beginTime-endTime));
						return retVal;*/
						
						advise.beforeMethod(method);
						Object retVal  = method.invoke(target,args);
						advise.afterMethod(method);
						
						return retVal;
					}
						
					}
				);
		return proxy3;
	}

}
